<?php
/**
 * Created by PhpStorm.
 * User: glq
 * Date: 2019/4/4
 * Time: 13:30
 */
namespace Yjsh\Controller;

use Common\Controller\AppframeController;
use Think\Cache\Driver\Redis;

class AdmincommunitystoreController extends AppframeController {
    private $page_size;

    function _initialize() {
        parent::_initialize();
        $this->page_size    = 5;
    }

    // 铺货团长列表
    public function cs_list()
    {
        $status = I('get.status', 0);
        $result = array();
        if ($status) {
            $count  = M('community')->where(['cs_status' => $status])->count();
            $page   = $this->page($count, $this->page_size);
            $lists  = M('community')
                ->alias('c')
                ->join("__USER__ u on c.head_uid = u.id")
                ->where(['c.cs_status' => $status])
                ->limit($page->firstRow , $page->listRows)
                ->field("c.id, c.name as community_name, c.head_uid, c.address, c.create_time, c.mobile, c.cs_apply_time, c.credit, u.nick_name")
                ->select();
            $result['status']   = 1;
            $result['pages']    = ceil($count / $this->page_size);
            $result['count']    = $count;
            $result['lists']    = $lists;
        } else {
            $result['msg'] = '检索类型有误';
        }
        $this->ajaxReturn($result);
    }

    // 同意成为可铺货团长
    public function set_community_status()
    {
        $c_id   = I('get.c_id', 0);
        $type   = I('get.type', 0); // 1-同意，2-禁止
        $result = array();
        if ($c_id && $type) {
            if ($type == 1) {
                $info = M('community')->where(['id' => $c_id])->find();
                if ($info['old_credit'] <= 0) {
                    // 新铺货团长
                    if (strtotime($info['create_time']) + 30 * 24 * 3600 > time()) {
                        // 未满一个月
                        $credit     = 119;
                        $old_credit = 119;
                        $new_credit = 119;
                    } else {
                        // 超过一个月
                        $start_time     = date("Y-m-d H:i:s", time() - 30 * 24 * 3600);
                        $total_amount   = M('order')->where(['commu_id' => $c_id, 'status' => ['GT', 1], 'pay_time' => ['GT', $start_time]])->sum('price');
                        $credit         = $total_amount / 26 * 0.6;
                        $old_credit     = $credit;
                        $new_credit     = $credit;
                    }
                } else {
                    // 之前曾经是铺货团长
                    $credit         = $info['credit'];
                    $old_credit     = $info['old_credit'];
                    $new_credit     = $info['new_credit'];
                }
                $data = ['cs_status' => 2, 'credit' => $credit, 'old_credit' => $old_credit, 'new_credit' => $new_credit];
            } else {
                $data = ['cs_status' => 3];
            }
            M('community')->where(['id' => $c_id])->save($data);
            $result['status'] = 1;
        } else {
            $result['msg'] = '参数错误';
        }
        $this->ajaxReturn($result);
    }

    // 设置团长信用额
    public function set_community_credit()
    {
        $c_id       = I('get.c_id', 0);
        $credit     = I('get.credit', 0);
        $result = array();
        if ($c_id && $credit) {
            M('community')->where(['id' => $c_id])->save(['credit' => $credit]);
            $result['status'] = 1;
        } else {
            $result['msg'] = '参数错误';
        }
        $this->ajaxReturn($result);
    }

    private function get_config()
    {
        $redis  = Redis::getinstance();
        $key    = "Yjsh_config";
        $re     = $redis->get($key);
        if ($re) {
            return $re;
        } else {
            $info = M('config')->where(['id' => 2])->find();
            if (!$info) {
                writeFile("GroupBuy get config error!!!!");
            } else {
                $redis->set($key, $info, 24 * 60 * 60);
            }
            return $info ?: [];
        }
    }
}